# @Time : 2020/8/26 14:57
# @Author : Fioman 
# @Phone : 13149920693
"""
numpy    直接加的话, 如果超过了0~255,使用取模运算作为结果.
opencv   使用add, 如果超过了0~255,使用0或者255来替代
"""
import cv2 as cv
import numpy as np

# 使用numpy直接加减的时候
print("超过了255 = {}".format(str(np.uint8([200]) + np.uint8([100]))))  # 300 % 256 取模
print("小于0 = {}".format(str(np.uint8([100]) - np.uint8([200]))))  # -100 % 256 取模

# 使用opencv的add和subtract,超过255取值为255,小于0取值为0
print("超过了255 = {}".format(str(cv.add(np.uint8([200]), np.uint8([100])))))
print("小于0 = {}".format(str(cv.subtract(np.uint8([100]), np.uint8([200])))))
image = cv.imread("pic/05.png")
M = np.ones(image.shape, dtype=np.uint8) * 100
added = cv.add(image, M)
cv.imshow("Added", added)
M = np.ones(image.shape, dtype=np.uint8) * 50
subtracted = cv.subtract(image, M)
cv.imshow("Subtracted", subtracted)
cv.waitKey(0)

image = cv.imread("pic/08.png")
M = np.ones(image.shape, dtype=np.uint8)*75
added = cv.add(image,M)
b,g,r = added[152,61]
print("R = {}, G = {}, B = {}".format(r,g,b))


rectangle = np.zeros((300,300),dtype=np.uint8)
cv.rectangle(rectangle,(25,25),(275,275),255,-1)
cv.imshow("Rectangle",rectangle)

circle = np.zeros((300,300),dtype=np.uint8)
cv.circle(circle,(150,150),150,255,-1)
cv.imshow("Circle",circle)
cv.waitKey(0)

bitwiseAnd = cv.bitwise_and(rectangle,circle)
cv.imshow("And",bitwiseAnd)
cv.waitKey(0)

bitwiseOr = cv.bitwise_or(rectangle,circle)
cv.imshow("Or",bitwiseOr)
cv.waitKey(0)

bitwiseNot = cv.bitwise_not(rectangle)
cv.imshow("Not",bitwiseNot)
cv.waitKey(0)

bitwiseXor = cv.bitwise_xor(rectangle,circle)
cv.imshow("Xor",bitwiseXor)
cv.waitKey(0)


image = cv.imread("pic/06.png")
cv.imshow("Original",image)
mask = np.zeros(image.shape[:2],dtype=np.uint8)
cv.rectangle(mask,(0,90),(290,450),255,-1)
cv.imshow("Mask",mask)

masked = cv.bitwise_and(image,image,mask=mask)
cv.imshow("UseMask",masked)
cv.waitKey(0)

# 使用一个圆形的掩模
mask = np.zeros(image.shape[:2],dtype=np.uint8)
cv.circle(mask,(145,200),100,255,-1)
masked = cv.bitwise_and(image,image,mask=mask)
cv.imshow("Mask",mask)
cv.imshow("UseMaskCircle",masked)
cv.waitKey(0)















































